package com.alstudio.utils.android.net;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.alstudio.utils.android.AndroidUtils;
import com.alstudio.utils.log.ALLog;

/**
 * ALWifiManager，封装了常用的获取wifi网络信息的方法。如： 获取Mac地址、获取BSSID、获取SSID、 获取附近可用的wifi热点、
 * 获取wifi模块启用状态、获取wifi模块当前状态
 * 
 * @author alonso lee
 * 
 */
public class ALWifiManager {

	// 此用于回掉通知获取到扫描结果
	private static WifiScanResultCallback mWifiScanResultCallback;
	private static WifiStateChangedCallback mWifiStateChangedCallback;

	public static boolean isWifiEnable(Context ctx) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		if (mWifiManager != null) {
			return mWifiManager.isWifiEnabled();
		}
		return false;
	}

	public static void toggleWifiModule(Context ctx) {
		boolean enabled = !isWifiEnable(ctx);
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		if (mWifiManager != null) {
			mWifiManager.setWifiEnabled(enabled);
		}
	}

	public static void toggleWifiModule(Context ctx,
			WifiStateChangedCallback callback) {
		mWifiStateChangedCallback = callback;
		registerWifiStateChangedReceiver(ctx);
		boolean enabled = isWifiEnable(ctx);
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		if (mWifiManager != null) {
			mWifiManager.setWifiEnabled(!enabled);
		}
	}

	public static int getWifiState(Context ctx) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		if (mWifiManager != null) {
			return mWifiManager.getWifiState();
		}
		return -1;
	}

	/**
	 * 获取Mac地址
	 * 
	 * @param ctx
	 * @return null表示当前无法获取到mac地址
	 */
	public static String getMacAddress(Context ctx) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		if (mWifiManager != null && mWifiManager.getConnectionInfo() != null) {
			return mWifiManager.getConnectionInfo().getMacAddress();
		}
		return null;
	}

	/**
	 * 获取附近的wifi热点
	 * 
	 * @param ctx
	 * @param callback
	 *            接收扫描wifi热点的回掉方法
	 */
	public static void getLocalWifiAP(Context ctx,
			WifiScanResultCallback callback) {
		mWifiScanResultCallback = callback;
		startScanWifiAp(ctx);
	}

	/**
	 * 扫描wifi热点
	 * 
	 * @param ctx
	 */
	private static void startScanWifiAp(Context ctx) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);

		if (mWifiManager != null) {
			if (mWifiManager.isWifiEnabled()) {
				// 注册监听wifi扫描结果的接收器
				registerWifiScanResultReceiver(ctx);
				// 开始扫描热点
				if (mWifiManager.startScan()) {
					ALLog.d("scanning local wifi ap...");
				} else {
					unregisterWifiScanResultReceiver(ctx);
					ALLog.e("scan fail!");
				}
			} else {
				ALLog.w("wifi module disable!");
			}
		} else {
			ALLog.e("no available module!");
		}
	}

	/**
	 * 获取当前bssid信息
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getCurrentBssid(Context ctx) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		String bssid = null;
		if (mWifiManager != null) {
			if (mWifiManager.isWifiEnabled()) {
				WifiInfo mInfo = mWifiManager.getConnectionInfo();
				if (mInfo != null) {
					bssid = mInfo.getBSSID();
					ALLog.d("current bssid: " + bssid);
				} else {
					ALLog.e("get bssid failed: no wifi connection");

				}
			} else {
				ALLog.w("get bssid failed: wifi module disable!");
			}
		} else {
			ALLog.e("get bssid failed: no available module!");
		}
		return bssid;
	}

	/**
	 * 获取当前wifi链接的ssid
	 * 
	 * @param ctx
	 * @return
	 */
	public static String getCurrentSsid(Context ctx) {

		WifiManager mWifiManager = AndroidUtils.getWifiManager(ctx);
		String ssid = null;
		if (mWifiManager != null) {
			if (mWifiManager.isWifiEnabled()) {
				WifiInfo mInfo = mWifiManager.getConnectionInfo();
				if (mInfo != null) {
					ssid = mInfo.getSSID();
					ALLog.d("current ssid: " + ssid);
				} else {
					ALLog.e("get ssid failed: no wifi connection");

				}
			} else {
				ALLog.w("get ssid failed: wifi module disable!");
			}
		} else {
			ALLog.e("get ssid failed: no available module!");
		}
		return ssid;
	}

	private static BroadcastReceiver wifiStateChangedReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {

				WifiManager wifiManager = (WifiManager) context
						.getSystemService(Context.WIFI_SERVICE);
				ALLog.d("Setting wifistate: " + wifiManager.getWifiState());

				// boolean state = false;
				// unregisterWifiStateChangedReceiver(context);
				// if (intent != null) {
				// int wifiState = intent.getIntExtra(
				// WifiManager.EXTRA_WIFI_STATE,
				// 0);
				// switch (wifiState) {
				// case WifiManager.WIFI_STATE_ENABLED:
				// state = true;
				// break;
				// }
				// }
				// if (mWifiStateChangedCallback != null) {
				// mWifiStateChangedCallback.onWifiStateChangedCallback(state);
				// }
			}
		}
	};

	/**
	 * 注册wifi开关结果广播接收器
	 * 
	 * @param ctx
	 */
	private static void registerWifiStateChangedReceiver(Context ctx) {
		ALLog.d("register wifi state change receiver");
		ctx.registerReceiver(wifiStateChangedReceiver, new IntentFilter(
				WifiManager.WIFI_STATE_CHANGED_ACTION));
	}

	/**
	 * 解除wifi开关结果广播接收器
	 * 
	 * @param ctx
	 */
	private static void unregisterWifiStateChangedReceiver(Context ctx) {
		ALLog.d("unregister wifi state change receiver");
		ctx.unregisterReceiver(wifiStateChangedReceiver);
	}

	private static BroadcastReceiver wifiScanResultReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub

			List<ScanResult> result = parseWifiScanResult(context, intent);
			if (mWifiScanResultCallback != null) {
				mWifiScanResultCallback.onGetWifiScanResult(result);
			}
		}
	};

	/**
	 * 注册wifi扫描结果广播接收器
	 * 
	 * @param ctx
	 */
	private static void registerWifiScanResultReceiver(Context ctx) {
		ALLog.d("register wifi scan result receiver");
		ctx.registerReceiver(wifiScanResultReceiver, new IntentFilter(
				WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
	}

	/**
	 * 接触wifi扫描结果广播接收器
	 * 
	 * @param ctx
	 */
	private static void unregisterWifiScanResultReceiver(Context ctx) {
		ALLog.d("unregister wifi scan result receiver");
		ctx.unregisterReceiver(wifiScanResultReceiver);
	}

	/**
	 * 解析扫描结果
	 * 
	 * @param context
	 * @param intent
	 * @return
	 */
	private static List<ScanResult> parseWifiScanResult(Context context,
			Intent intent) {
		WifiManager mWifiManager = AndroidUtils.getWifiManager(context);
		List<ScanResult> list = null;
		unregisterWifiScanResultReceiver(context);
		if (mWifiManager != null) {
			list = mWifiManager.getScanResults();
			int size = list.size();

			if (size == 0) {
				ALLog.e("no wifi ap");
				return null;
			}

			ScanResult info = null;

			for (int i = 0; i < size; i++) {
				info = list.get(i);
				if (info != null) {
					ALLog.e("get local wifi ap: " + info.SSID);
				}
			}
		}
		return list;
	}

	public interface WifiScanResultCallback {
		public void onGetWifiScanResult(List<ScanResult> resultList);
	}

	public interface WifiStateChangedCallback {
		public void onWifiStateChangedCallback(boolean state);
	}
}
